package com.zhiyou.servlet;

import com.zhiyou.model.Rent;
import com.zhiyou.model.User;
import com.zhiyou.service.UserListService;
import com.zhiyou.service.impl.UserLisetServiceImp;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.text.ParseException;
import java.text.SimpleDateFormat;

public class UserUpdateServlet extends HttpServlet {
    private UserListService uLS = new UserLisetServiceImp();
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        User user = new User();
        //接受数据
        String uid = req.getParameter("uid");
        String uname = req.getParameter("uname");
        String upassword = req.getParameter("upassword");
        String urealname = req.getParameter("urealname");
        String uroid = req.getParameter("uroid");
        String uaddTime = req.getParameter("uaddTime");
        String ustatus = req.getParameter("ustatus");
        //封装
        user.setUid(Integer.parseInt(uid));
        user.setUname(uname);
        user.setUpassword(upassword);
        user.setUrealname(urealname);
        user.setUroid(Integer.parseInt(uroid));
        user.setUstatus(Integer.parseInt(ustatus));
        try {
            user.setUaddTime(new SimpleDateFormat("yy-MM-dd").parse(uaddTime));
        } catch (ParseException e) {
            e.printStackTrace();
        }
        uLS.updateNew(user);
        resp.sendRedirect(req.getContextPath()+"/user/list?pageNo="+req.getParameter("pageNo"));
    }

    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        System.out.println("更新执行");
        String idStr = req.getParameter("uid");
        int uid=Integer.parseInt(idStr);
        //根据id查房租信息
        User user = uLS.selectById(uid);
        //将查询到的房租信息放入session域中
        req.getSession().setAttribute("user",user);

        //接受参数判断是更新还是详情
        String detail = req.getParameter("detail");
        if (detail!=null){
            resp.sendRedirect(req.getContextPath()+"/view/user/detail.jsp");
        }else{
            resp.sendRedirect(req.getContextPath()+"/view/user/edit.jsp");
        }

    }
}
